/**
 *  *2010-6-17 下午10:59:47  add 
 */
package com.base.myproject.client.HR.Self;

import com.base.myproject.client.busi.MenuCommandFactory;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Administrator
 *2010-6-17
 * javazgw@gmail.com
 */
public class SelfPanel extends LayoutContainer{
	LayoutContainer panel = new LayoutContainer();
	TableData td = new TableData();
	public SelfPanel()
	{
		init();
	}

private void init() {
		
		initwidget();
		load();
	}

	void load() {
		GreetingServiceAsync greetingService = GWT
				.create(GreetingService.class);
		greetingService.getDataSet("select  menuname,command from menu where self>0 order by firstsetting ", new AsyncCallback<DataSet>() {

			@Override
			public void onSuccess(DataSet result) {
				for (int i=0;i<result.getRowCount();i++) {

					
					// Anchor anchor = new Anchor(result.getValue(i, "mununame"),false);
					GWT.log(result.getValue(i, "mununame"));
					GWT.log(result.getValue(i, 1));
					 Button b = new Button(result.getValue(i, "menuname"));
					 b.setScale(ButtonScale.LARGE);
					 b.setData("munucommand", result.getValue(i, "command"));
					 b.addSelectionListener(getListener());
					 panel.add(b);
					 panel.layout();
					 b.addSelectionListener(new SelectionListener<ButtonEvent>() {
						
						@Override
						public void componentSelected(ButtonEvent ce) {
							// TODO Auto-generated method stub
							
						}
					});
					 
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("错误", "网络连接失败", null);

			}
		});

	}

	private void initwidget() {

		
		// panel.setHeaderVisible(false);
		// panel.setAutoHeight(true);
		// panel.setAutoWidth(true);

		
		Label label4 = new Label("备注");

		TableLayout tl = new TableLayout(3);

		tl.setCellPadding(20);
		tl.setCellSpacing(20);
		td.setRowspan(1);
		td.setColspan(1);
		td.setPadding(10);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.MIDDLE);

		panel.setLayout(tl);
		
	

		// panel.setHeaderVisible(false);
		panel.setSize(400, 400);
		this.setLayout(new CenterLayout());
		// BorderLayoutData centerData = new
		// BorderLayoutData(LayoutRegion.CENTER);
		//
		//		 
		// centerData.setMargins(new Margins(5, 5, 5, 5));
		this.add(panel);
		// this.add(new Button("sssss"),centerData);

	}

	int sync = 0;
	SelectionListener<ButtonEvent> defaultlistener;

	SelectionListener<ButtonEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				String munucommand =ce.getButton().getData("munucommand");
				MenuCommandFactory mcf =new MenuCommandFactory(munucommand);
				mcf.getCommand(munucommand);
				
			}
		};
		return defaultlistener;
		
	}
}
